<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模版编号" prop="priceTypeCode">
        <el-input
          v-model="queryParams.priceTypeCode"
          placeholder="请输入模版编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模版名称" prop="priceTypeName">
        <el-input
          v-model="queryParams.priceTypeName"
          placeholder="请输入模版名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="组织机构" prop="deptId">
              <el-input
                v-model="queryParams.deptId"
                placeholder="请输入组织机构"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['operator:priceType:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operator:priceType:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['operator:priceType:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="resetQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="priceTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="模版ID" align="center" prop="priceTypeId" />
      <el-table-column label="模版编号" align="center" prop="priceTypeCode"/>-->
      <el-table-column label="模版名称" align="center" prop="priceTypeName"/>
      <el-table-column label="运营商名称" align="center" prop="operatorName"/>
      <el-table-column label="场站名称" align="center" prop="stationName"/>
      <el-table-column label="基础电价" align="center" prop="foundationElectricCharge"/>
      <el-table-column label="基础服务费" align="center" prop="foundationServiceFee"/>
      <el-table-column label="基础停车费" align="center" prop="foundationStopFee"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="组织机构" align="center" prop="deptId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-right"
            @click="viewPriceTypeInfo(scope.row)"
            v-hasPermi="['system:price:list','system:price:add']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operator:priceType:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operator:priceType:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改价格模版对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
       <!-- <el-form-item label="模版编号" prop="priceTypeCode">
          <el-input v-model="form.priceTypeCode" placeholder="请输入模版编号"/>
        </el-form-item>-->
        <el-form-item label="模版名称" prop="priceTypeName">
          <el-input v-model="form.priceTypeName" placeholder="请输入模版名称"/>
        </el-form-item>
        <el-form-item label="运营商" prop="operatorCode">
          <el-select v-model="form.operatorCode" filterable style="width: 340px" placeholder="请选择运营商"
                     @change="getStationListByCode()">
            <el-option
              v-for="item in operatorList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点" prop="chargeStationCode">
          <el-select v-model="form.chargeStationCode" filterable style="width: 340px" placeholder="请选择站点">
            <el-option
              v-for="item in stationList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="基础电价" prop="originalPrice">
          <el-input  v-model="form.foundationElectricCharge" placeholder="请输入基础电价" style="width: 340px"/>
        </el-form-item>
        <el-form-item label="基础服务费" prop="serviceFee">
          <el-input  v-model="form.foundationServiceFee" placeholder="请输入基础服务费" style="width: 340px"/>
        </el-form-item>
        <el-form-item label="基础停车费" prop="stopFee">
          <el-input  v-model="form.foundationStopFee" placeholder="请输入基础停车费" style="width: 340px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listPriceType, getPriceType, delPriceType, addPriceType, updatePriceType} from "@/api/smart/operator/priceType";
  import {getOperatorList, getStationListByCode} from "@/api/smart/operator/electricityPrice";

  export default {
    name: "PriceType",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 子表选中数据
        checkedChargeStationPrice: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 价格模版表格数据
        priceTypeList: [],
        // 场站电区间价设置表格数据
        chargeStationPriceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //服务商列表
        operatorList: [],
        //服务商列表
        stationList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          priceTypeCode: null,
          priceTypeName: null,
          deptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      /*查询运营商列表*/
      this.getOperatorList()
      this.getList();
    },
    methods: {
      /*查询运营商列表*/
      getOperatorList() {
        getOperatorList().then(response => {
          this.operatorList = response.data;
        })
      },
      /*查询场站列表*/
      getStationListByCode() {
        let param = {"code": this.form.operatorCode}
        getStationListByCode(param).then(response => {
          this.stationList = response.data;
        })
      },
      /** 查询价格模版列表 */
      getList() {
        this.loading = true;
        listPriceType(this.queryParams).then(response => {
          this.priceTypeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          priceTypeId: null,
          priceTypeCode: null,
          priceTypeName: null,
          deptId: null,
          operatorCode: null,
          chargeStationCode: null,
          foundationElectricCharge: null,
          foundationServiceFee:null,
          foundationStopFee:null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.chargeStationPriceList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.priceTypeId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加价格模版";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const priceTypeId = row.priceTypeId || this.ids
        getPriceType(priceTypeId).then(response => {
          //查询站点信息
          let param = {"code": response.data.operatorCode}
          this.form = response.data;
          this.chargeStationPriceList = response.data.chargeStationPriceList;
          getStationListByCode(param).then(response => {
            this.stationList = response.data;
            this.open = true;
            this.title = "修改场站电区间价设置";
          })
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.chargeStationPriceList = this.chargeStationPriceList;
            if (this.form.priceTypeId != null) {
              updatePriceType(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPriceType(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const priceTypeIds = row.priceTypeId || this.ids;
        const priceTypeNames = row.priceTypeName || this.names
        this.$modal.confirm('是否确认删除模版名称为"' + priceTypeNames + '"的数据项？').then(function () {
          return delPriceType(priceTypeIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 场站电区间价设置序号 */
      rowChargeStationPriceIndex({row, rowIndex}) {
        row.index = rowIndex + 1;
      },
      /** 场站电区间价设置添加按钮操作 */
      handleAddChargeStationPrice() {
        let obj = {};
        obj.strategyId = "";
        obj.deviceId = "";
        obj.operatorId = "";
        obj.stationId = "";
        obj.startTime = "";
        obj.endTime = "";
        obj.electricCharge = "";
        obj.originalPrice = "";
        obj.serviceFee = "";
        obj.stopFee = "";
        obj.createName = "";
        obj.updateName = "";
        this.chargeStationPriceList.push(obj);
      },
      /** 场站电区间价设置删除按钮操作 */
      handleDeleteChargeStationPrice() {
        if (this.checkedChargeStationPrice.length == 0) {
          this.$modal.msgError("请先选择要删除的场站电区间价设置数据");
        } else {
          const chargeStationPriceList = this.chargeStationPriceList;
          const checkedChargeStationPrice = this.checkedChargeStationPrice;
          this.chargeStationPriceList = chargeStationPriceList.filter(function (item) {
            return checkedChargeStationPrice.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleChargeStationPriceSelectionChange(selection) {
        this.checkedChargeStationPrice = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('operator/priceType/export', {
          ...this.queryParams
        }, `priceType_${new Date().getTime()}.xlsx`)
      },
      /*查看设备详情*/
      viewPriceTypeInfo(row) {
        this.$router.push({
          name: 'viewPriceTypeInfo',
          query: {
            priceTypeCode: row.priceTypeCode,priceTypeId: row.priceTypeId
          }
        })
      },
    }
  };
</script>
